package com.lphj.datainterface.handle;

import com.lphj.datainterface.component.Config.ReadCustomerPriceConfig;
import com.lphj.datainterface.component.Config.ReadOtherConfig;
import com.lphj.datainterface.entity.*;
import com.lphj.datainterface.service.CustomerService;
import com.lphj.datainterface.service.TCumQuotationDtlService;
import com.lphj.datainterface.service.TCumQuotationService;
import com.lphj.datainterface.service.TGoodsService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class TCustomerPriceForGMHandle {

    @Autowired
    ReadCustomerPriceConfig readCustomerPriceConfig;

    @Autowired
    CustomerService customerService;

    @Autowired
    ReadOtherConfig readOtherConfig;

    @Autowired
    TGoodsService tGoodsService;

    @Autowired
    TCumQuotationService tCumQuotationService;

    @Autowired
    TCumQuotationDtlService tCumQuotationDtlService;

    @Transactional
    public int addTCustomerPriceForGM(Sheet sheet) {
        int c = 0;
        boolean isNotNull = true;
        //获取标题
        Row firstRow = sheet.getRow(0);
        int index = 0;
        //获取首行标题
        List<String> firstRowTitle = new ArrayList<String>();

        if(sheet != null) {
            //获取首行标题
            while(isNotNull) {
                Cell cell = firstRow.getCell(index);
                if(cell != null) {
                    firstRowTitle.add(cell.getStringCellValue());
                } else {
                    isNotNull = false;
                }
                index ++;
            }
            int rcp_goodsId=0;
            int rcp_customer=0;
            int rcp_price = 0;
            int rcp_spec = 0;

            for(int j=0;j<firstRowTitle.size();j++) {
                if(firstRowTitle.get(j).equals(readCustomerPriceConfig.getGoodsId())) {
                    rcp_goodsId = j;
                } else if(firstRowTitle.get(j).equals(readCustomerPriceConfig.getCustomer())) {
                    rcp_customer = j;
                } else if(firstRowTitle.get(j).equals(readCustomerPriceConfig.getPrice())) {
                    rcp_price = j;
                } else if(firstRowTitle.get(j).equals(readCustomerPriceConfig.getSpec())) {
                    rcp_spec = j;
                }
            }
            //获取所有客户
            List<Customer> customerList = customerService.getCustomerByAll();
            //创建单元格
            Cell cell;
            for(int i=0;i<customerList.size();i++) {
                Customer customer = customerList.get(i);
                int count = 0;
                //客户报价单
                TCumQuotation tCumQuotation = new TCumQuotation();
                tCumQuotation.setsPaperNO(OrderIdHandle.getOrderId(i + "",new Date()));
                tCumQuotation.setsCumNO(customer.getCumNO());
                tCumQuotation.setsStoreNO(readOtherConfig.getStoreNO());
                tCumQuotation.setsConfirmUser("管理员");
                tCumQuotation.setsCreateUser("管理员");
                tCumQuotation.setnTag(100);
                tCumQuotation.setnTag2(1);
                tCumQuotation.setsRefPaperNO(tCumQuotation.getsPaperNO());
                //循环获取报价明细单
                for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                    //读取第r行
                    Row row = sheet.getRow(r);
                    if (row == null){
                        continue;
                    }
                    //获取ERP客户名称
                    String customerName = "";
                    cell = row.getCell(rcp_customer);
                    if(cell != null) {
                        customerName = cell.getStringCellValue();
                    }
                    cell = null;
                    //比较表格里的客户名称是否和数据库现有的客户名称匹配
                    if(customer.getCumName().equals(customerName)) {
                        //获取商品编号
                        cell = row.getCell(rcp_goodsId);
                        String goodsNO2 = "";
                        if(cell != null) {
                            goodsNO2 = cell.getStringCellValue();
                        }
                        //获取商品销售规格
                        cell = row.getCell(rcp_spec);
                        String spec = "";
                        if(cell != null) {
                            spec = cell.getStringCellValue();
                        }

                        //获取商品报价
                        cell = row.getCell(rcp_price);
                        double price = 0;
                        if(cell != null) {
                            cell.setCellType(CellType.STRING);
                            price = Double.parseDouble(cell.getStringCellValue());
                        }
                        //判断数据库里是否有该商品信息
                        //System.out.println(goodsNO2);
                        TGoods tGoods = tGoodsService.getGoodsBySGoodsNO2(goodsNO2,spec);
                        if(tGoods != null) {
                            //客户报价单明细
                            TCumQuotationDtl tCumQuotationDtl = new TCumQuotationDtl();
                            tCumQuotationDtl.setsPaperNO(tCumQuotation.getsPaperNO());
                            tCumQuotationDtl.setnGoodsID(tGoods.getGoodsID());
                            tCumQuotationDtl.setnPrice(price);
                            count += tCumQuotationDtlService.addTCumQuotationDtl(tCumQuotationDtl);
                        }
                    }
                }
                if(count > 0) {
                    c += tCumQuotationService.addTCumQuotation(tCumQuotation);
                }
            }
        }
        return c;
    }
}
